package tech.abing.spider.task.crawler.csrc;

import java.util.Arrays;
import java.util.List;

import tech.abing.spider.CrawTaskBuilder;
import tech.abing.spider.CrawlManager;
import tech.abing.spider.CrawlTask;
import tech.abing.spider.component.DynamicEntrance;
import tech.abing.spider.component.listener.TaskLifeListener;
import tech.abing.spider.downloader.impl.DefaultPageDownloader;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.PageRequest.PageEncoding;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.constants.DictEnum;
import tech.abing.spider.task.constants.TaskConstants;
import tech.abing.spider.task.pageprocessor.csrc.CommissionDataProcessor;
import tech.abing.spider.task.pipeline.csrc.CommissionDataPipeline;
import tech.abing.spider.util.SqlUtil;

/**
 * 证监会数据（行政处罚决定、市场禁入决定、责令整改）抓取
 * @author Ivan qb.huang@wescxx.com   
 * @date 2016-11-2 下午02:13:40 
 * @version V2.0
 */
public class CommissionDataSpider {

	private static SqlUtil sqlUtil = null;
	/**
	 * @param args
	 */
	public static void start(){

		CrawlManager manager = CrawlManager.getInstance();
		
		CrawTaskBuilder builder = manager
		.prepareCrawlTask("证监会数据（行政处罚决定、市场禁入决定、责令整改）", DefaultPageDownloader.class)
		.useThread(10)
		.useDynamicEntrance(DynamicEntranceImpl.class)
		.usePageEncoding(PageEncoding.UTF8)
		.usePageRetryCount(3)
		.usePipeline(CommissionDataPipeline.class)
		.useTaskLifeListener(new TaskLifeListener(){

			@Override
			public void onStart(CrawlTask task) {
				System.out.println("================开始");
				if(sqlUtil==null){
					sqlUtil = SqlUtil.getInstance();
				}
				sqlUtil.deleteForConn("delete from "+DictEnum.TASK_TYPE_COMMISSION_DATA.getCode());
			}

			@Override
			public void onFinished(CrawlTask task) {
				task.getPipeline().destory();
				sqlUtil.closeConn();
				sqlUtil = null;
				System.out.println("================完成");
			}
			
		});
		
		CrawlTask task = builder.build();
		
		manager.start(task);
		
	}
	
	public static final class DynamicEntranceImpl extends DynamicEntrance{

		@Override
		public List<StartContext> loadStartContext() {
			
			StartContext context = new StartContext();
			
			String pageNameDefine = TaskConstants.COMMISSION_INDEX_PAGE + "." + TaskConstants.PAGE_SUBFIX;
			//http://www.csrc.gov.cn/pub/zjhpublic/3300/3619/index_7401.htm  市场禁入决定
			String url1 = TaskConstants.COMMISSION_MAIN_PAGE+"/3300/3619/"+pageNameDefine;
			PageRequest req1 = context.createPageRequest(url1, CommissionDataProcessor.class);
			req1.putParams("infoType", TaskConstants.INFO_TYPE_DEBAR);
			context.injectSeed(req1);
			
			//http://www.csrc.gov.cn/pub/zjhpublic/3300/3313/index_7401.htm  行政处罚决定
			String url2 = TaskConstants.COMMISSION_MAIN_PAGE+"/3300/3313/"+pageNameDefine;
			PageRequest req2 = context.createPageRequest(url2, CommissionDataProcessor.class);
			req2.putParams("infoType", TaskConstants.INFO_TYPE_PUNISH);
			context.injectSeed(req2);
			
			//http://www.csrc.gov.cn/pub/zjhpublic/3300/3312/3402/index_7401.htm  责令整改
			String url3 = TaskConstants.COMMISSION_MAIN_PAGE+"/3300/3312/3402/"+pageNameDefine;
			PageRequest req3 = context.createPageRequest(url3, CommissionDataProcessor.class);
			req3.putParams("infoType", TaskConstants.INFO_TYPE_RECTIFY);
			context.injectSeed(req3);
			
			return Arrays.asList(context);
		}
		
	}

}
